/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gestionmagasins_aspectj_junit_patterns.model;

public class Usager 
{
    private String firstname;
    private String lastname;
    private String username;
    private String password;
    private String email;
    
    private Role role;
    
    private boolean activated;

    /**
     * @return the firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) 
    {
        if(firstname == null)
            throw new IllegalArgumentException("Parameter must not be null!");
        
        this.firstname = firstname;
    }

    /**
     * @return the lastname
     */
    public String getLastname() {
        return lastname;
    }

    /**
     * @param lastname the lastname to set
     */
    public void setLastname(String lastname) 
    {    
        if(lastname == null)
            throw new IllegalArgumentException("Parameter must not be null!");

        this.lastname = lastname;
        
    }

    /**
     * @return the username
     */
    public String getUsername() 
    {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) 
    {
        if(username == null)
            throw new IllegalArgumentException("Parameter must not be null!");
        
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) 
    {
        if(password == null)
            throw new IllegalArgumentException("Parameter must not be null!");
        
        this.password = password;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) 
    {    
        if(email == null)
            throw new IllegalArgumentException("Parameter must not be null!");
        
        this.email = email;
    }
    
    /**
     * @return the role
     */
    public Role getRole() {
        return role;
    }

    /**
     * @param role the role to set
     */
    public void setRole(Role role) 
    {
        if(role == null)
            throw new IllegalArgumentException("Parameter must not be null!");
        
        this.role = role;
    }
    
    /**
     * @return the activated
     */
    public boolean isActivated() {
        return activated;
    }

    /**
     * @param activated the activated to set
     */
    public void setActivated(boolean activated) {
        this.activated = activated;
    }
    
    private String getActivatedString(boolean showActivatedString)
    {
        return (activated ? (showActivatedString ? "[actif]" : "") : "[inactif]");
    }
    
    public Usager() 
    {        
        this.firstname = "";
        this.lastname = "";
        this.username = "";
        this.password = "";
        this.email = "";
        
        this.role = CatalogueRoles.ROLE_AUCUN;
        
        this.activated = true;
    }
    
    public Usager(String firstname, String lastname, String username, String password, String email, Role role) 
    {
        
        if(    firstname == null
            || lastname == null
            || username == null
            || password == null
            || email == null
            || role == null)
        {
            throw new IllegalArgumentException("Parameter must not be null!");
        }
        
        this.firstname = firstname;
        this.lastname = lastname;
        this.username = username;
        this.password = password;
        this.email = email;
        
        this.role = role;
        
        this.activated = true;
    }
    
    public boolean matches(String criteria) 
    {
        criteria = criteria.toLowerCase();
        
        return (   firstname.toLowerCase().contains(criteria) 
                || lastname.toLowerCase().contains(criteria) 
                || email.toLowerCase().contains(criteria) 
                || username.toLowerCase().contains(criteria)
                || getActivatedString(true).toLowerCase().contains(criteria));
    }
    
    @Override
    public String toString() 
    {
        return String.format("%s, %s - %s %s", lastname, firstname, email, getActivatedString(false));
    }

}
